<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/5/11 0011
 * Time: 19:59
 */
namespace Wx\Controller;
use Think\Controller;
class ListController extends Controller
{
    public function Index(){
		$shop_spellgroup=D('shop_spellgroup');//拼团表
		$shop_spellgroup_copy=D('shop_spellgroup_copy');//小区副本
		$category=D('category');
		$member=D('member');
		
		//普通分类
		$all_c=$category->where("pid=31")->select();
		$this->assign('all_c',$all_c);
		//点击普通分类
		if($_GET['type_id']>0){
			$where_typeid="type_id=".$_GET['type_id']." and ";
			$this->assign('type_id',$_GET['type_id']);
		}//end  if
		//全部产品 ————————————————————————————
		$page=1;
		if($_GET['page']>1)$page=$_GET['page'];//瀑布流调用
		$shoparr=$shop_spellgroup->page($page.',6')->where($where_typeid."state=1 and type=0")->order("starttime asc")->select(); // 查询第一页数据
		//dump(session());die;
		//查询当前用户小区id
		if(session('u_id')>0) {
			$getuser=$member->where("id=".session('u_id'))->find();//当前用户信息
			$this->assign('user',$getuser);
			
            $getstore = M("shop_store")->where("c_id = {$getuser['c_id']}")->find();//获取该社区绑定店铺
			$this->assign('store',$getstore);
			
            $getcommunity = M("shop_community")->where("id = {$getuser['c_id']}")->find();//获取该社区信息
			$this->assign('community',$getcommunity);
			
			foreach($shoparr as $i=>$one){
				if($getuser["c_id"]){
					//已经绑定小区
					//用户本小区拼团信息
					$getcopy=$shop_spellgroup_copy->where("sg_id=".$one['id']." and c_id=".$getuser["c_id"])->find();//用户本小区该商品是否开团
					if($getcopy){
						$shoparr[$i]['havecopy']=1;//表示已经开团
						if($one['min_num']- $getcopy['num']>0){
							$shoparr[$i]['neednum']=$one['min_num']- $getcopy['num'];//用户本小区还差几人份成团
							}else{
								$shoparr[$i]['neednum']=0;//拼团成功，还可以继续参团
						}//end if
						
					}else{
							$shoparr[$i]['havecopy']=0;//未开团
							
					}//end if
				}else{
					//未绑定小区
					$shoparr[$i]['havecopy']=0;//未开团
				}//end if
				//单位 
				$getu=$category->where("id=".$one['unit'])->find();//dump($getu);
				$shoparr[$i]['unit']=$getu['title'];
				//产品第一图片
				$img=explode(',',$one['h_imgs']);
				$shoparr[$i]['f_img']=$img[0];//第一张图片作为封面图
				//发布人头像、姓名
				if($one['u_id']>0)$getinfo=$member->where("id=".$one['u_id'])->find();
				$shoparr[$i]['f_realname']=$getinfo['realname'];
				$shoparr[$i]['f_avatar']=$getinfo['avatar'];
				$shoparr[$i]['actual_num']=$shop_spellgroup_copy->where("sg_id=".$one['id'])->sum('num');//已售多少份
				
			}//end foreach
			//全部产品 ————————————————————————————end
			//即将成团（本小区）————————————————————————————————
			if($getuser["c_id"]>0){
				$getneedgood=$shop_spellgroup_copy->where("orsuccess=0 and state=0 and c_id=".$getuser["c_id"])->select();//dump($getneedgood);die;
				foreach($getneedgood as $k=>$one){
					$getgood=$shop_spellgroup->where("id=".$one['sg_id'])->find();
					//首页图、类型、标题、规格、拼团价、sg_id、发布人头像姓名、已售多少份、还差多少成团
					//产品第一图片 
					$img=explode(',',$getgood['h_imgs']);
					$getneedgood[$k]['f_img']=$img[0];//第一张图片作为封面图
					$getneedgood[$k]['type']=$getgood['type'];
					$getneedgood[$k]['title']=$getgood['title'];
					$getneedgood[$k]['u_num']=$getgood['u_num'];
					$getu=$category->where("id=".$getgood['unit'])->find();
					$getneedgood[$k]['unit']=$getu['title'];
					$getneedgood[$k]['actual_price']=$getgood['actual_price'];
					//发布人头像、姓名
					if($one['u_id']>0)$getinfo=$member->where("id=".$one['u_id'])->find();
					$getneedgood[$k]['f_realname']=$getinfo['realname'];
					$getneedgood[$k]['f_avatar']=$getinfo['avatar'];
					
					$getneedgood[$k]['actual_num']=$shop_spellgroup_copy->where("sg_id=".$one['sg_id'])->sum('num');//已售多少份
					$getneedgood[$k]['neednum']=$getgood['min_num']-$one['num'];
				}//end foreach
				//dump($getneedgood);
				$this->assign('needgoodarr',$getneedgood);
			}//end if
			//即将成团（本小区）————————————————————————————————end
		}//end if
		$this->assign('shoparr',$shoparr);
		if($_GET['page']>1)$this->ajaxReturn($shoparr,'json');//瀑布流调用返回
		//新上架产品——————————————————————————————————
		$newshoparr=$shop_spellgroup->page('1,10')->where($where_typeid."state=1 and (".time().">starttime and ".time()."<endtime)")->order("starttime asc")->select(); // 查询第一页数据
		//dump(session());die;
		//查询当前用户小区id
		if(session('u_id')>0) {
			$getuser=$member->where("id=".session('u_id'))->find();//当前用户信息
			$this->assign('user',$getuser);
			
            $getstore = M("shop_store")->where("c_id = {$getuser['c_id']}")->find();//获取该社区绑定店铺
			$this->assign('store',$getstore);
			
            $getcommunity = M("shop_community")->where("id = {$getuser['c_id']}")->find();//获取该社区信息
			$this->assign('community',$getcommunity);
			
			foreach($newshoparr as $i=>$one){
				if($getuser["c_id"]){
					//已经绑定小区
					//用户本小区拼团信息
					$getcopy=$shop_spellgroup_copy->where("sg_id=".$one['id']." and c_id=".$getuser["c_id"])->find();//用户本小区该商品是否开团
					if($getcopy){
						$newshoparr[$i]['havecopy']=1;//表示已经开团
						if($one['min_num']- $getcopy['num']>0){
							$newshoparr[$i]['neednum']=$one['min_num']- $getcopy['num'];//用户本小区还差几人份成团
							}else{
								$newshoparr[$i]['neednum']=0;//拼团成功，还可以继续参团
						}//end if
						
					}else{
							$newshoparr[$i]['havecopy']=0;//未开团
							
					}//end if
				}else{
					//未绑定小区
					$newshoparr[$i]['havecopy']=0;//未开团
				}//end if
				//产品第一图片
				$img=explode(',',$one['h_imgs']);
				$newshoparr[$i]['f_img']=$img[0];//第一张图片作为封面图
				//发布人头像、姓名
				if($one['u_id']>0)$getinfo=$member->where("id=".$one['u_id'])->find();
				$newshoparr[$i]['f_realname']=$getinfo['realname'];
				$newshoparr[$i]['f_avatar']=$getinfo['avatar'];
				$newshoparr[$i]['actual_num']=$shop_spellgroup_copy->where("sg_id=".$one['id'])->sum('num');//已售多少份
				
			}//end foreach
		}//endif
		//新上架产品——————————————————————————————————end
		$this->assign('newshoparr',$newshoparr);
		//即将上架————————————————————————————
		$willgood=$shop_spellgroup->page('1,10')->where($where_typeid."state=1 and starttime>".time())->order("starttime asc")->select(); // 查询第一页数据
		foreach($willgood as $k=>$one){
			$getgood=$shop_spellgroup->where("id=".$one['id'])->find();
			//首页图、类型、标题、规格、拼团价、sg_id、发布人头像姓名
			//产品第一图片 
			$img=explode(',',$getgood['h_imgs']);
			$willgood[$k]['f_img']=$img[0];//第一张图片作为封面图
			$willgood[$k]['type']=$getgood['type'];
			$willgood[$k]['title']=$getgood['title'];
			$willgood[$k]['u_num']=$getgood['u_num'];
			$getu=$category->where("id=".$getgood['unit'])->find();
			$willgood[$k]['unit']=$getu['title'];
			$willgood[$k]['actual_price']=$getgood['actual_price'];
			//发布人头像、姓名
			if($one['u_id']>0)$getinfo=$member->where("id=".$one['u_id'])->find();
			$willgood[$k]['f_realname']=$getinfo['realname'];
			$willgood[$k]['f_avatar']=$getinfo['avatar'];
			//上架时间、关注人数
			$willgood[$k]['starttime']=$getgood['starttime'];
		}//end foreach
		$this->assign('willgood',$willgood);
		//即将上架————————————————————————————end
        $this->display('list');
    }//end fun index
	//搜索
	public function search(){
		$shop_spellgroup=D('shop_spellgroup');//拼团表
		$shop_spellgroup_copy=D('shop_spellgroup_copy');//小区副本
		$category=D('category');
		$member=D('member');
		//地标产品搜索
		if($_GET['area_keyword']!=''){
			$area_where="`type_name` LIKE '%".$_GET['area_keyword']."%' and ";
		}else{
			$where="";
		}//end if
		//关键词搜索
		if($_GET['keyword']!=''){
			$where="`title` LIKE '%".$_GET['keyword']."%' and ";
		}else{
			$where="";
		}//end if
		//______________________________
		$shoparr=$shop_spellgroup->where($area_where.$where."state=1")->order("starttime asc")->select(); // 查询第一页数据
		//dump(session());die;
		//查询当前用户小区id
		if(session('u_id')>0) {
			$getuser=$member->where("id=".session('u_id'))->find();//当前用户信息
			$this->assign('user',$getuser);
			
            $getstore = M("shop_store")->where("c_id = {$getuser['c_id']}")->find();//获取该社区绑定店铺
			$this->assign('store',$getstore);
			
            $getcommunity = M("shop_community")->where("id = {$getuser['c_id']}")->find();//获取该社区信息
			$this->assign('community',$getcommunity);
			
			foreach($shoparr as $i=>$one){
				if($getuser["c_id"]){
					//已经绑定小区
					//用户本小区拼团信息
					$getcopy=$shop_spellgroup_copy->where("sg_id=".$one['id']." and c_id=".$getuser["c_id"])->find();//用户本小区该商品是否开团
					if($getcopy){
						$shoparr[$i]['havecopy']=1;//表示已经开团
						if($one['min_num']- $getcopy['num']>0){
							$shoparr[$i]['neednum']=$one['min_num']- $getcopy['num'];//用户本小区还差几人份成团
							}else{
								$shoparr[$i]['neednum']=0;//拼团成功，还可以继续参团
						}//end if
						
					}else{
							$shoparr[$i]['havecopy']=0;//未开团
							
					}//end if
				}else{
					//未绑定小区
					$shoparr[$i]['havecopy']=0;//未开团
				}//end if
				//单位 
				$getu=$category->where("id=".$one['unit'])->find();//dump($getu);
				$shoparr[$i]['unit']=$getu['title'];
				//产品第一图片
				$img=explode(',',$one['h_imgs']);
				$shoparr[$i]['f_img']=$img[0];//第一张图片作为封面图
				//发布人头像、姓名
				if($one['u_id']>0)$getinfo=$member->where("id=".$one['u_id'])->find();
				$shoparr[$i]['f_realname']=$getinfo['realname'];
				$shoparr[$i]['f_avatar']=$getinfo['avatar'];
				$shoparr[$i]['actual_num']=$shop_spellgroup_copy->where("sg_id=".$one['id'])->sum('num');//已售多少份
				
			}//end foreach
		}//end if
		if(count($shoparr)==0)$this->assign('haveno',1);//查不到产品
		$this->assign('keyword',$_GET['keyword']);
		$this->assign('shoparr',$shoparr);
		//______________________________
		$this->display();
	}//end fun
	
}//end class